package com.fourinone.utils;

import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

import javax.swing.*;
import java.util.ArrayList;
import java.util.Properties;
import java.util.jar.Attributes;

public class XmlParse extends DefaultHandler {
    private boolean textFlag = false;
    private ArrayList propsAl;
    private Properties curProps;
    private String curKey;
    private String PROP_DESC;
    private String KEY_DESC;
    private String curPROP_DESC;
    private String curKEY_DESC;
    public XmlParse() {

    }
    public void startDocument() throws SAXException {

    }
    public void startElement(String uri, String sName, String qName, Attributes attrs) {
        if (qName.equals("PROPS")) {
            propsAl = new ArrayList();
        }
        else if (qName.equals("PROP")) {
            curPROP_DESC = attrs.getValue("DESC");
            curProps = new Properties();
        }
        else {
            curKEY_DESC = attrs.getValue("DESC");
            curKey = qName;
            textFlag = true;
        }
    }
    public void characters(char[] data, int start, int length) {
        String content = new String(data, start, length);
        if(textFlag) {
            if (KEY_DESC == null || (curKEY_DESC != null && curKEY_DESC.equals(KEY_DESC))) {
                curProps.put(curKey, content.trim());
            }
        }
    }
    public void endElement(String uri, String sName, String qName) {
        if(qName.equals("PROPS")) {

        }
        else if (qName.equals("PROP")) {
            if (PROP_DESC == null || (curPROP_DESC != null && curPROP_DESC.equals(PROP_DESC))) {
                propsAl.add(curProps);
            }
        }
        else {
            textFlag = false;
        }
    }
    public void endDocument() {

    }
    public ArrayList getPropsAl() {
        return propsAl;
    }
    public void setPROP_DESC(String PROP_DESC) {
        this.PROP_DESC = PROP_DESC;
    }
    public void setKEY_DESC (String KEY_DESC) {
        this.KEY_DESC = KEY_DESC;
    }
}
